﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data;

namespace CardLogistics.Web.MainPage.PutOutCard.PutOutCard
{
    /// <summary>
    /// GetInStorageDetail 的摘要说明
    /// </summary>
    public class GetInStorageDetail : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string strRKId = context.Request.QueryString["RKId"];//入库单ID
            string strCardNo = context.Request.QueryString["CardNo"];//盒号
            if (strRKId.Trim().Length > 0 || strRKId.Trim().Length > 0)
            {
                //string strSql = "SELECT * FROM v_putincarddetailedrpt where ID='" + strRKId + "'";
                StringBuilder strSql = new StringBuilder();
                string strStartNOsql = "(select min(c.cardno) from cardrecord c where c.cardhno=i.cardno and BCPRUKUID is not null and CPCHUID is null)";//开始卡号
                string strEndNOsql = "(select max(c.cardno) from cardrecord c where c.cardhno=i.cardno and BCPRUKUID is not null and CPCHUID is null)";//结束卡号
                strSql.Append("SELECT ID,CARDNO," + strStartNOsql + " as STARTNO," + strEndNOsql + "as ENDNO,");
                strSql.Append("(select (max(CardNo)- min(CardNo)+1) from cardrecord c where c.cardhno=i.cardno and BCPRUKUID is not null and CPCHUID is null) as Duenumber,");
                strSql.Append("(select count(*) from cardrecord c where c.cardhno=i.cardno and BCPRUKUID is not null and CPCHUID is null) as ACTUNUMBER,");
                strSql.Append("(select count(*) from cardrecord_lack l where l.lcardno between " + strStartNOsql + " and " + strEndNOsql + " )as MISSINGCARDNO,");
                strSql.Append(" FlagName,FGVAMT,InitAmt FROM v_putincarddetailedrpt i ");
                strSql.Append(" where i.ID='" + strRKId + "'");
                //获取半成品入库明细
                DataTable dt = DAL.DbHelperSQL.GetQueryResult(strSql.ToString());
                string retContent = "";//返回字符串
                if (dt != null && dt.Rows.Count > 0)
                {
                    // 半成品入库ID;CARDNO盒号;STARTNO起始卡号;ENDNO结束卡号;Duenumber应有数量/张;ACTUNUMBER实际数量/张;MISSINGCARDNO缺卡卡号;FlagName是否启用;FGVAMT押金;InitAmt预充值
                    retContent = dt.Rows[0]["Id"].ToString() + "," + dt.Rows[0]["CARDNO"].ToString() + ";" + dt.Rows[0]["CARDNO"].ToString() + ";" + dt.Rows[0]["STARTNO"].ToString() + ";" + dt.Rows[0]["ENDNO"].ToString() + ";" + dt.Rows[0]["Duenumber"].ToString() + ";" + dt.Rows[0]["ACTUNUMBER"].ToString() + ";" + dt.Rows[0]["MISSINGCARDNO"].ToString() + ";" + dt.Rows[0]["FlagName"].ToString() + ";" + dt.Rows[0]["FGVAMT"].ToString() + ";" + dt.Rows[0]["InitAmt"].ToString(); 
                }
                context.Response.Write(retContent);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}